﻿@page "/buckets"
@using InfluxDB.Client.Api.Domain
@using ExampleBlazor.Data
@using Radzen

@inject IJSRuntime JsRuntime

<PageTitle>Buckets</PageTitle>

<div class="row my-xl-3">
    <div class="col-xl-12">
        <RadzenCard class="pb-5 fill">
            <div class="row">
                <div class="col-6">
                    <h2 class="h1_blue">Buckets<span class="h1_pink">Registration</span></h2>
                </div>
                <div class="col-6 text-end">
                    <input class="card-select" placeholder="Bucket Name" value="@_stringValue"
                           @oninput="eventArgs => { SetNewBucketValue(eventArgs.Value as string); }"/>
                    <RadzenButton Click="CreateBucket" Icon="add_circle_outline" IsBusy="@CreateBucketBusy"
                                  ButtonStyle="ButtonStyle.Light"/>
                    <RadzenButton Click="RefreshBuckets" Icon="refresh" IsBusy="@RefreshBucketsBusy"
                                  ButtonStyle="ButtonStyle.Light"/>
                </div>
            </div>
            <hr/>

            <div class="row">
                <div class="col-12">
                    <RadzenDataGrid Data="@_bucketList" TItem="Bucket" PagerPosition="PagerPosition.Bottom" AllowPaging="true" AllowSorting="false">
                        <Columns>
                            <RadzenDataGridColumn TItem="Bucket" Property="Name" Title="Name">
                                <Template Context="bucket">
                                    @bucket.Name
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Bucket" Property="ID" Title="ID">
                                <Template Context="bucket">
                                    @bucket.Id
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Bucket" Property="CreatedAt" Title="Created at">
                                <Template Context="bucket">
                                    @bucket.CreatedAt
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Bucket" Property="Description" Title="Description">
                                <Template Context="bucket">
                                    @bucket.Description
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Bucket" Property="OrgId" Title="OrgID">
                                <Template Context="bucket">
                                    @bucket.OrgID
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Bucket" Property="OrgId" Title="Actions">
                                <Template Context="bucket">
                                    <RadzenButton Click="() => DeleteBucket(bucket)" Icon="delete"
                                                  ButtonStyle="ButtonStyle.Light"/>
                                    <RadzenButton Click="() => CloneBucket(bucket)" Icon="control_point_duplicate"
                                                  ButtonStyle="ButtonStyle.Light"/>
                                </Template>
                            </RadzenDataGridColumn>

                        </Columns>
                    </RadzenDataGrid>
                </div>
            </div>
        </RadzenCard>
    </div>
</div>


@code {
    private List<Bucket>? _bucketList;

    string? _stringValue = "";

    private bool _createBucketBusy;

    private bool CreateBucketBusy
    {
        get => _createBucketBusy;
        set
        {
            _createBucketBusy = value;
            StateHasChanged();
        }
    }

    private bool _refreshBucketsBusy;

    private bool RefreshBucketsBusy
    {
        get => _refreshBucketsBusy;
        set
        {
            _refreshBucketsBusy = value;
            StateHasChanged();
        }
    }

    private void SetNewBucketValue(string? value)
    {
        _stringValue = value;
    }

    protected override async Task OnInitializedAsync()
    {
        await RefreshBuckets();
    }

    private async Task RefreshBuckets()
    {
        RefreshBucketsBusy = true;
        _bucketList = await InfluxModel.FetchBuckets();
        RefreshBucketsBusy = false;
    }

    private async Task DeleteBucket(Bucket bucket)
    {
        var confirmed = await JsRuntime.InvokeAsync<bool>("confirm", $"Delete bucket {bucket.Name}?");
        if (confirmed)
        {
            await InfluxModel.DeleteBucket(bucket);
            await JsRuntime.InvokeAsync<bool>("confirm", "Bucket was deleted.");
            await RefreshBuckets();
        }
    }

    private async Task CloneBucket(Bucket bucket)
    {
        var confirmed = await JsRuntime.InvokeAsync<bool>("confirm", $"Clone bucket {bucket.Name}?");
        if (confirmed)
        {
            var name = bucket.Name + "_clone";
            if (_bucketList != null && _bucketList.Exists(item => item.Name == name))
            {
                var append = 2;
                while (_bucketList.Exists(item => item.Name == name + append))
                {
                    append++;
                }
                name += append;
            }

            await InfluxModel.CloneBucket(bucket, name);
            await JsRuntime.InvokeAsync<bool>("confirm", "Bucket was cloned.");
            await RefreshBuckets();
        }
    }

    private async Task CreateBucket()
    {
        CreateBucketBusy = true;
        if (_stringValue != null && _stringValue.Any())
        {
            var bucket = await InfluxModel.CreateBucket(_stringValue);

            if (bucket != null)
            {
                await JsRuntime.InvokeAsync<bool>("confirm", $"Bucket {bucket.Name} was created.");
                SetNewBucketValue("");
                await RefreshBuckets();
            }
        }
        else
        {
            await JsRuntime.InvokeAsync<bool>("confirm", "Bucket Name cannot be empty.");
        }
        CreateBucketBusy = false;
    }

}